using System.Linq;
using Shop.Backend.Api.Entity;
using Shop.Backend.Api.Params;
using Shop.Backend.Api.Repository;
using Shop.Backend.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace Shop.Backend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    //这里的控制器是 Suppliers
    public class SuppliersController : ControllerBase
    {
        private IConfiguration _configuration;
        private IRepository<Users> _usersRepository;
        private IRepository<Goods> _goodsRepository;
        private IRepository<Shops> _shopsRepository;
        private IRepository<Suppliers> _suppliersRepository;

        private TokenParameter _tokenParameter;


        public SuppliersController(IConfiguration configuration
        , IRepository<Users> usersRepository
        , IRepository<Goods> goodsRepository
        , IRepository<Shops> shopsRepository
        , IRepository<Suppliers> suppliersRepository)
        {
            _configuration = configuration;
            _usersRepository = usersRepository;
            _goodsRepository = goodsRepository;
            _shopsRepository = shopsRepository;
            _suppliersRepository = suppliersRepository;
            _tokenParameter =
                _configuration
                    .GetSection("TokenParameter")
                    .Get<TokenParameter>();
        }


        [HttpGet]
        public dynamic Get([FromQuery] QueryWithPager query)
        {
            // get请求默认从url中获取参数，如果需要使用实体接收参数，需要FromQuery特性
            var pageIndex = query.Pager.PageIndex;
            var pageSize = query.Pager.PageSize;

            var keyword = string.IsNullOrEmpty(query.Keyword) ? "" : query.Keyword.Trim();

            var suppliers = _suppliersRepository.Table.Where(x => x.IsDeleted == false); ;

            // 如果keyword不为空，则再去查询用户名
            if (!string.IsNullOrEmpty(keyword))
            {
                suppliers = suppliers.Where(x => x.Id.ToString() == keyword
              || x.SuppliersName.Contains(keyword)
              || x.SuppliersPhone.Contains(keyword)
              || x.SuppliersAddress.Contains(keyword)); //未完善的模糊查询
            }
            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = new
                {
                    Data = suppliers,
                    Pager = new
                    {
                        pageIndex,
                        pageSize,
                        rowsTotal = suppliers.Count()
                    }
                },
                Msg = "获取供应商成功^_^"
            });
        }

        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var suppliers = _suppliersRepository.GetById(id);
            return new
            {
                Code = 1000,
                Data = suppliers,
                Msg = "获取指定供货商成功^_^"
            };
        }

        //修改供应商状态的方法
        [HttpPut,Route("status")]
        public dynamic PutStatus(CreateSupplier updateSupplier)
        {
            var id = updateSupplier.id;
            var user = _suppliersRepository.GetById(id);
            
            
            if (user == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "要更新的供应商不存在，请确认后重试=_="
                };
            }

            user.IsActived = updateSupplier.IsActived;
            _suppliersRepository.Update(user);

            return new
            {
                Code = 1000,
                Data = user,
                Msg = "更新供应商状态成功"
            };
        }


        [HttpPost]
        public dynamic Post(CreateSupplier createSupplier)
        {
            var fromUsersId = createSupplier.FromUsersId;
            var suppliersName = createSupplier.SuppliersName.Trim();
            var suppliersAddress = createSupplier.SuppliersAddress.Trim();
            var suppliersPhone = createSupplier.SuppliersPhone.Trim();

            if (string.IsNullOrEmpty(suppliersName))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "供应商名不能为空"
                };
            }
            if (string.IsNullOrEmpty(suppliersAddress))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "地址不能为空"
                };
            }
            if (string.IsNullOrEmpty(suppliersPhone))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "联系方式不能为空"
                };
            }

            var supplier = new Suppliers
            {
                FromUsersId = createSupplier.FromUsersId,
                SuppliersName = createSupplier.SuppliersName,
                SuppliersAddress = createSupplier.SuppliersAddress,
                SuppliersPhone = createSupplier.SuppliersPhone
            };

            _suppliersRepository.Insert(supplier);
            return new
            {
                Code = 1000,
                Data = supplier,
                Msg = "添加成为供应商成功^_^"
            };
        }
        //编辑指定供应商
        [HttpPut("{id}")]
        public dynamic Put(int id, CreateSupplier updateSupplier)//未完善
        {
            var suppliersName = updateSupplier.SuppliersName.Trim();
            var suppliersAddress = updateSupplier.SuppliersAddress.Trim();
            var suppliersPhone = updateSupplier.SuppliersPhone.Trim();
            if (string.IsNullOrEmpty(suppliersName)
            && string.IsNullOrEmpty(suppliersName)
            && string.IsNullOrEmpty(suppliersName))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "供应商名字或地址或电话不能为空，请检查后重试‘。’"
                };

            }
            var supplier = _suppliersRepository.GetById(id);
            if (supplier == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "要编辑的供应商不存在"
                };
            }
            supplier.SuppliersName = updateSupplier.SuppliersName;
            supplier.SuppliersAddress = updateSupplier.SuppliersAddress;
            supplier.SuppliersPhone = updateSupplier.SuppliersPhone;

            _suppliersRepository.Update(supplier);
            return new
            {
                Code = 1000,
                Data = supplier,
                Msg = "编辑供应商成功^_^"
            };

        }

        //删除OR禁用该供应商
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {

            var supplier = _suppliersRepository.GetById(id);
            if (supplier == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "要删除的供应商不存在"
                };
            }
            supplier.IsActived = false;
            supplier.IsDeleted = true;

            _suppliersRepository.Update(supplier);

            return new
            {
                Code = 1000,
                Data = "",
                Msg = "删除供应商成功^_^"
            };
        }

    }
}